package com.kahweh.ngphoto.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kahweh.ngphoto.bean.KProperty;
import com.kahweh.ngphoto.client.PropertyService;
import com.kahweh.ngphoto.client.bean.Property;
import com.kahweh.ngphoto.db.PMF;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class PropertyServiceImpl extends RemoteServiceServlet implements PropertyService {

	/* (non-Javadoc)
	 * @see com.kahweh.ngphoto.client.KPropertyService#KPropertyService(int, com.kahweh.ngphoto.client.bean.KProperty)
	 */
	@Override
	public List<Property> propertyService(int actionType, Property property) {
		UserService us = UserServiceFactory.getUserService();
		User u = us.getCurrentUser();
		if (u == null || !"kahweh@gmail.com".equalsIgnoreCase(u.getEmail())) {
			return null;
		}

		PersistenceManager pm;
		switch (actionType) {
		case 0:
			//Cretae new KProperty
			pm = PMF.get().getPersistenceManager();
			String query = "select from " + KProperty.class.getName() + 
				" where section == sectionParam &&" +
				" name == nameParam " +
				" parameters String sectionParam, String nameParam";
			try {
				List<KProperty> properties = (List<KProperty>)pm.newQuery(query)
					.execute(property.getSection(), property.getName());
				if (properties.isEmpty()) {
					pm.makePersistent(getKProperty(property));
				} else {
					KProperty p = properties.get(0);
					p.setType(property.getType());
					p.setValue(property.getValue());
					pm.makePersistent(p);
				}
				
			} finally {
				pm.close();
			}
			break;
		case 1:
			//Delete KProperty
			pm = PMF.get().getPersistenceManager();
			query = "select from " + KProperty.class.getName() + 
				" where key == idParam parameters long idParam";
			try {
				List<KProperty> properties = (List<KProperty>)pm.newQuery(query).execute(property.getId());
				if (!properties.isEmpty()) {
					for (KProperty p : properties) {
						pm.deletePersistent(p);
					}
				}
			} finally {
				pm.close();
			}
			break;
		case 2:
			//Update do nothing
			break;
		default:
			break;
		}
		pm = PMF.get().getPersistenceManager();
		String query = "select from " + KProperty.class.getName() + " order by section";
		List<Property> rtn = new ArrayList<Property>();
		try {
			List<KProperty> list = (List<KProperty>)pm.newQuery(query).execute();
			for (KProperty p : list) {
				rtn.add(getProperty(p));
			}
		} finally {
			pm.close();
		}
		return rtn;
	}
	
	private KProperty getKProperty(Property p) {
		return new KProperty(p.getSection(), p.getName(), p.getType(), p.getValue());
	}
	
	private Property getProperty(KProperty p) {
		return new Property(p.getKey().getId(), p.getSection(), p.getName(), p.getType(), p.getValue());
	}
}
